package com.iplatform.iplatformuser.privilege.api;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iplatform.commons.pagination.Pagination;
import com.iplatform.commons.utils.UserUtil;
import com.iplatform.iplatformuser.common.ConstantUtil;
import com.iplatform.iplatformuser.common.ResponseMsg;
import com.iplatform.iplatformuser.framework.springmvc.BaseController;
import com.iplatform.iplatformuser.framework.util.UniqueIDGenerator;
import com.iplatform.iplatformuser.privilege.service.inter.IPriOptService;
import com.iplatform.module.privilege.PriManagerBean;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 操作管理接口
 * @author heng
 *
 */
@RestController
@RequestMapping("/api/v1/opt")
@Api(tags = "操作管理接口")
public class ApiPriOptController extends BaseController {

	@Autowired
	public IPriOptService priOptService;


	/**
	 * 操作列表
	 * @param formMap
	 * @return
	 */
	@ApiOperation(value = "操作列表")
	@ApiImplicitParams({
		@ApiImplicitParam(dataType = "String", name = "optName", value = "名称(不传值就是查询所有,传值为模糊查询)", required = false, paramType = "query") })
	@ApiResponses({ @ApiResponse(code = HttpServletResponse.SC_OK, message = "成功"),
		@ApiResponse(code = HttpServletResponse.SC_CONFLICT, message = "没有做到API使用规范") })
	@GetMapping(value = "/list")
	public ResponseEntity<PageInfo<Map<String, String>>> list(@ApiParam(hidden = true) @RequestParam Map<String, String> formMap,
                                                              @ApiParam(hidden = true) @ModelAttribute(value = "pagination") Pagination pagination) {

		PageHelper.startPage(pagination.getPageNum(), pagination.getPageSize());
		List<Map<String, String>> resultList = priOptService.queryOpt(formMap);
		PageInfo<Map<String, String>> pageInfo = new PageInfo<Map<String, String>>(resultList);

		return new ResponseEntity<>(pageInfo, HttpStatus.OK);

	}


	/**
	 * 新增操作管理
	 * @param formMap
	 * @return
	 */
	@ApiOperation(value = "新增操作管理")
	@ApiImplicitParams({
		@ApiImplicitParam(dataType = "String", name = "optName", value = "名称", required = true, paramType = "query"),
		@ApiImplicitParam(dataType = "String", name = "optCode", value = "操作代码", required = true, paramType = "query", example="1"),
		@ApiImplicitParam(dataType = "String", name = "description", value = "描述", required = false, paramType = "query"),
		@ApiImplicitParam(dataType = "String", name = "status", value = "状态N为可用，Y为禁用", required = true, paramType = "query"),
		@ApiImplicitParam(dataType = "String", name = "remark", value = "开发备注", required = false, paramType = "query"),
		@ApiImplicitParam(dataType = "BigDecimal", name = "version", value = "版本(不传默认为1)", required = false, paramType = "query")
		})
	@ApiResponses({ @ApiResponse(code = HttpServletResponse.SC_OK, message = "成功"),
		@ApiResponse(code = HttpServletResponse.SC_CONFLICT, message = "没有做到API使用规范") })
	@PostMapping()
	public ResponseEntity<ResponseMsg> add(@ApiParam(hidden = true) @RequestParam Map<String, String> formMap) {
		PriManagerBean pri = UserUtil.getLoginAppUser();

		if(StringUtils.isEmpty(formMap.get("description"))) {
			formMap.put("description", "");
		}
		if(StringUtils.isEmpty(formMap.get("remark"))) {
			formMap.put("remark", "");
		}
		if (StringUtils.isEmpty(formMap.get("version"))){
			formMap.put("version","1");
		}
		//TODO 获取当前用户
		if (pri != null){
			formMap.put("createdBy",pri.getUserCode());
		}else {
			formMap.put("createdBy", "admin");
		}
		formMap.put("optId", UniqueIDGenerator.getUUID());
		formMap.put("isDeleted","N");

		priOptService.insert(formMap);

		return new ResponseEntity<>(new ResponseMsg(ConstantUtil.SUCCESS), HttpStatus.OK);
	}



	/**
	 * 更新操作管理
	 * @param formMap
	 * @return
	 */
	@ApiOperation(value = "更新操作管理")
	@ApiImplicitParams({
		@ApiImplicitParam(dataType = "String", name = "optId", value = "ID", required = true, paramType = "query"),
		@ApiImplicitParam(dataType = "String", name = "optName", value = "名称", required = true, paramType = "query"),
		@ApiImplicitParam(dataType = "int", name = "optCode", value = "操作代码", required = true, paramType = "query", example="1"),
		@ApiImplicitParam(dataType = "String", name = "optOrder", value = "顺序", required = true, paramType = "query"),
		@ApiImplicitParam(dataType = "String", name = "description", value = "描述", required = false, paramType = "query"),
		@ApiImplicitParam(dataType = "String", name = "status", value = "状态N为可用，Y为禁用", required = true, paramType = "query")
		})
	@ApiResponses({ @ApiResponse(code = HttpServletResponse.SC_OK, message = "成功"),
		@ApiResponse(code = HttpServletResponse.SC_CONFLICT, message = "没有做到API使用规范") })
	@PutMapping()
	public ResponseEntity<ResponseMsg> update(@ApiParam(hidden = true) @RequestParam Map<String, String> formMap) {
		PriManagerBean pri = UserUtil.getLoginAppUser();
		//TODO 获取当前用户
		if (pri != null){
			formMap.put("UPDATED_BY",pri.getUserCode());
		}else {
			formMap.put("UPDATED_BY", "admin");
		}

		priOptService.update(formMap);

		return new ResponseEntity<>(new ResponseMsg(ConstantUtil.SUCCESS), HttpStatus.OK);
	}

	/**
	 * 批量删除操作管理
	 * @param deleteIds
	 * @return
	 */
	@ApiOperation(value = "批量删除操作管理")
	@ApiImplicitParams({
		@ApiImplicitParam(dataType = "String", name = "deleteIds", value = "删除的ID集合，多个以逗号分隔", required = true, paramType = "query") })
	@ApiResponses({ @ApiResponse(code = HttpServletResponse.SC_OK, message = "成功"),
		@ApiResponse(code = HttpServletResponse.SC_CONFLICT, message = "没有做到API使用规范") })
	@PutMapping(value = "/batchDelete")
	public ResponseEntity<ResponseMsg> batchDelete(@RequestParam String deleteIds) {

		priOptService.batchDelete(deleteIds.split(","));

		return new ResponseEntity<>(new ResponseMsg(ConstantUtil.SUCCESS), HttpStatus.OK);
	}

	/**
	 * 修改操作管理状态
	 * @param optId
	 * @return
	 */
	@ApiOperation(value = "修改操作管理状态")
	@ApiImplicitParams({
		@ApiImplicitParam(dataType = "String", name = "optId", value = "id", required = true, paramType = "query") })
	@ApiResponses({ @ApiResponse(code = HttpServletResponse.SC_OK, message = "成功"),
		@ApiResponse(code = HttpServletResponse.SC_CONFLICT, message = "没有做到API使用规范") })
	@PutMapping(value = "/setIsStop")
	public ResponseEntity<ResponseMsg> setIsStop(@RequestParam String optId) {

		priOptService.setIsStop(optId);

		return new ResponseEntity<>(new ResponseMsg(ConstantUtil.SUCCESS), HttpStatus.OK);
	}


	/**
	 * 查询操作名称
	 * @return
	 */
	@ApiOperation(value = "查询操作名称")
	@RequestMapping(value = "/queryOptNameList", method = RequestMethod.POST)
	public ResponseEntity<JSONObject> queryOptNameList() {

		List<Map<String, String>> resultList = priOptService.queryName();

		JSONObject allObject = new JSONObject();
		JSONArray arrayName = new JSONArray();

		for(Map<String, String> nameList : resultList) {
			JSONObject objectName = new JSONObject();
			objectName.put("optName", nameList.get("optName"));
			arrayName.add(objectName);
		}
		allObject.put("name", arrayName);
		return new ResponseEntity<>(allObject, HttpStatus.OK);
	}

}
